package listbyorder.access001_100.test38;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/5/31 9:58
 */
public class Solution1 {

    // 方法一： 自己手撸
    // 首先递归获取n-1生成字符串结果，然后对last进行改造即可

    public String countAndSay(int n) {
        if (n == 1) return "1";
        String last = countAndSay(n - 1);
        return getAns(last);
    }

    private String getAns(String last) {
        String ans = "";
        char cur = last.charAt(0);
        int count = 0;
        for (char c : last.toCharArray()) {
            if (c == cur) {
                count++;
            } else {
                ans += count + "" + cur;
                cur = c;
                count = 0;
            }
        }
        ans += count + "" + cur;
        return ans;
    }
}
